#!/usr/bin/env python3

import re

# 读取文件
with open('src/services/transport_service.rs', 'r') as f:
    content = f.read()

# 修复 f64 -> Decimal 的转换（在 Set() 中）
content = re.sub(r'Set\(req\.max_weight\)', 'Set(Decimal::from_f64_retain(req.max_weight).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.max_volume\)', 'Set(Decimal::from_f64_retain(req.max_volume).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.weight\)', 'Set(Decimal::from_f64_retain(req.weight).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.volume\)', 'Set(Decimal::from_f64_retain(req.volume).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.latitude\)', 'Set(Decimal::from_f64_retain(req.latitude).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.longitude\)', 'Set(Decimal::from_f64_retain(req.longitude).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.speed\)', 'Set(Decimal::from_f64_retain(req.speed).unwrap_or_default())', content)
content = re.sub(r'Set\(req\.heading\)', 'Set(Decimal::from_f64_retain(req.heading).unwrap_or_default())', content)

# 修复 f64 -> Decimal 的转换（在 update 中）
content = re.sub(r'Set\(max_weight\)', 'Set(Decimal::from_f64_retain(max_weight).unwrap_or_default())', content)
content = re.sub(r'Set\(max_volume\)', 'Set(Decimal::from_f64_retain(max_volume).unwrap_or_default())', content)

# 修复 Decimal -> f64 的转换（在响应中）
content = re.sub(r'max_weight: result\.max_weight,', 'max_weight: result.max_weight.to_f64().unwrap_or_default(),', content)
content = re.sub(r'max_volume: result\.max_volume,', 'max_volume: result.max_volume.to_f64().unwrap_or_default(),', content)
content = re.sub(r'weight: result\.weight,', 'weight: result.weight.to_f64().unwrap_or_default(),', content)
content = re.sub(r'volume: result\.volume,', 'volume: result.volume.to_f64().unwrap_or_default(),', content)

content = re.sub(r'max_weight: vehicle\.max_weight,', 'max_weight: vehicle.max_weight.to_f64().unwrap_or_default(),', content)
content = re.sub(r'max_volume: vehicle\.max_volume,', 'max_volume: vehicle.max_volume.to_f64().unwrap_or_default(),', content)
content = re.sub(r'weight: order\.weight,', 'weight: order.weight.to_f64().unwrap_or_default(),', content)
content = re.sub(r'volume: order\.volume,', 'volume: order.volume.to_f64().unwrap_or_default(),', content)

content = re.sub(r'latitude: location\.latitude,', 'latitude: location.latitude.to_f64().unwrap_or_default(),', content)
content = re.sub(r'longitude: location\.longitude,', 'longitude: location.longitude.to_f64().unwrap_or_default(),', content)
content = re.sub(r'speed: location\.speed,', 'speed: location.speed.to_f64().unwrap_or_default(),', content)
content = re.sub(r'heading: location\.heading,', 'heading: location.heading.to_f64().unwrap_or_default(),', content)

# 写回文件
with open('src/services/transport_service.rs', 'w') as f:
    f.write(content)

print("Fixed decimal conversions!")